import javafx.event.ActionEvent;
import javafx.event.EventTarget;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import java.util.Arrays;

import javafx.scene.control.Label;

public class Controller {
//    public void login(ActionEvent event) {
//        System.out.println("7 click");
//    }

    @FXML
    private Label expression;
    @FXML
    private Label result;

    private boolean lastIsNumber = true;
    private String currToken;
    int prev = 0;
    int curr = 0;
    private int number1;
    private int number2;

    String[] oneOperator = {"="};
    String[] twoOperator = {"+", "-", "×", "÷"};
    String lastOperator = "=";

    @FXML
    public void onClick(ActionEvent actionEvent) {
        EventTarget target = actionEvent.getTarget();
        Button button = (Button) target;
        String text = button.getText();
        Object ccc = button.getUserData();
        System.out.println("2525: " + ccc);
        String resultVal;
        try {
            if (!lastIsNumber) {
                result.setText("");
                resultVal = "";
            } else {
                resultVal = result.getText();
            }
//            resultVal = result.getText();
            prev = Integer.parseInt(resultVal + "0");
            int num = Integer.parseInt(text);
            curr = prev * 10 + num;
            result.setText(curr + "");
            if (Arrays.asList(oneOperator).contains(lastOperator)) {
                number1 = curr;
            }
            if (Arrays.asList(twoOperator).contains(lastOperator)) {
                number2 = curr;
            }
            lastIsNumber = true;
        } catch (NumberFormatException e) {
            lastIsNumber = false;
            currToken = text;
            prev = 0;
            curr = 0;
            result.setText("");
            switch (text) {
                case "+":
                    currToken = text;
                    break;
                case "-":
                    currToken = text;
                    break;
                case "=":
                    System.out.println("62:  " + number1 + " == " + number2);
//                    result.setText(number1 + number2 + "");
                    int num = 0;
                    if ("+".equals(lastOperator)) {
                        num = number1 + number2;
                    }
                    if ("-".equals(lastOperator)) {
                        num = number1 - number2;
                    }
                    if ("×".equals(lastOperator)) {
                        num = number1 * number2;
                    }
                    if ("÷".equals(lastOperator)) {
                        if (number2 == 0) {
                            System.out.println("除数不能为零");
                            return;
                        }
                        num = number1 / number2;
                    }
                    result.setText(num + "");
                    number1 = num;
//                    number2 = 0;
                    lastIsNumber = false;
                    break;
                case "1/x":
                    float reciprocal = (float) 1 / number1;
                    result.setText(reciprocal + "");
                    break;
                case "%":
                    float percentage = (float) number1 / 100;
                    result.setText(percentage + "");
                    break;
                case "x²":
                    System.out.println("109:  " + number1 + " == " + number2);
                    float square = (float) Math.pow(number1, 2);
                    result.setText(square + "");
                    break;
                case "√x":
                    float sqrt = (float) Math.sqrt(number1);
                    result.setText(sqrt + "");
                    break;
                case "C":
                    number2 = 0;
                    result.setText("");
                    break;
                default:
                    break;
            }
            lastOperator = text;
//            throw new RuntimeException(e);
        }
    }
}
